#include "space\engines\client.h"
#include "space\debug\logger.h"

#include <boost/date_time/posix_time/posix_time_types.hpp>

namespace space {
	namespace engines {

		E_ENGINE_STATE client::state()  {
			if(_shutdown)
				return E_ENGINE_STATE::STOPPING;
			if(_paused)
				return E_ENGINE_STATE::PAUSED;

			return E_ENGINE_STATE::RUNNING;
		}

		result client::shutdown() { 
			TRACE("Shutdown called");
			_shutdown = true;

			return RESULT_OK; 

		}
		result client::pause() { 
			TRACE("Pause called");
			_paused = true;

			return RESULT_OK; 
		}
		result client::unpause() { 
			TRACE("Unpause called");
			_paused = false;

			return RESULT_OK; 
		}

		result client::run() {
			LOG("Client engine entry");

			// Initialize segments here
			//  kick up all our managers and spin them up
			LOG("Engine initializing...");

			//
			// Main loop
			for(_running = true;
				_running && !_shutdown; 
			) {
			

				if(!_paused) {
					// simulation not paused
				} else {
					// simulation is paused
				}

			}

			return RESULT_OK;
		}

		client::client() : 
			_running(false),
			_paused(false),
			_shutdown(false)
		{
			// initialize the logging engine
			_initialize_text_log();
			
		}

		client::~client() {
			// TODO: Why does this fail./
			//space::debug::logger::release_all();
		}
		
		void client::_initialize_text_log(void) {
			_log_stream.open("space.log", std::ofstream::out | std::ofstream::app);
			_logger = shared_ptr<space::debug::logger>( space::debug::logger::create(
				space::debug::log_level::trace | space::debug::log_level::code | space::debug::log_level::critical | space::debug::log_level::debug | space::debug::log_level::info, 
				space::debug::log_verbosity::extreme, 
				_log_stream) );
		}
	};
};

